<?php
/**
 * Created by PhpStorm.
 * User: yandian
 * Date: 2017/4/21
 * Time: 20:54
 */

namespace app\weixin\controller;


use app\common\controller\LiteBase;
use app\common\model\AlbumType;
use app\common\model\Photo;

class Album extends LiteBase
{
    private function getData()
    {
        $query = \app\common\model\Album::instance()
            ->gotoPage()
            ->order('sort desc,id desc');

        $type = I('get.type', 0, 'intval');
        $time = I('get.time', 0, 'intval');
        if ($type) {
            $query->where(['type_id' => $type]);
        }
        if ($time) {
            switch ($time) {
                case 1: //一周内
                    $query->where(['TO_DAYS(now()) - TO_DAYS(create_time)' => ['lt', 8]]);
                    break;
                case 2:
                    $query->where(['TO_DAYS(now()) - TO_DAYS(create_time)' => ['lt', 31]]);
                    break;
                case 3:
                    $query->where(['TO_DAYS(now()) - TO_DAYS(create_time)' => ['lt', 184]]);
                    break;
            }
        }
        $rows = $query->select();
        $photo = Photo::instance();
        $host = ccnn_request_baseurl();
        foreach ($rows as &$row) {
            $row['photos'] = $photo->where(['album_id' => $row['id']])->getField('img_src', true);
            if (empty($row['photos'])) {
                $row['photos'] = [];
            }
            array_unshift($row['photos'], $row['img_src']);
            foreach ($row['photos'] as $k => $v) {
                $row['photos'][$k] = $host . $v;
            }
            $row['photos'] = json_encode($row['photos']);
        }
        return $rows;
    }

    public function index()
    {
        $this->assign('album_types', AlbumType::instance()->select());
        $this->assign('m', $this->getData());
        return $this->fetch();
    }

    public function jsonList()
    {
        return $this->jsonSuccess($this->getData());
    }

    public function detail()
    {
        return $this->fetch();
    }
}